//
//  MHomeViewController.swift
//  iOSweibo
//
//  Created by lzm on 16/5/9.
//  Copyright © 2016年 lzm. All rights reserved.
//

import UIKit

class MHomeViewController: MBaseTableVC {

    private let id = "cell"
    //更新列表显示数据
    var status : [MStatus]?{
        didSet{
            self.tableView.reloadData()
        }
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        if !MUserAccountViewModel.sharedInstance.isUserLogin{
            return
        }
        //注册cell
//        self.tableView.registerClass(M_StatusCell.self, forCellReuseIdentifier: id)
        tableView.registerClass(M_StatusCell.self, forCellReuseIdentifier: id)
        //去除cell的下划线
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        //设置行高
        tableView.estimatedRowHeight = 200
        tableView.rowHeight = UITableViewAutomaticDimension
        
        
        //判断用户是否用户
        if !MUserAccountViewModel.sharedInstance.isUserLogin{
            return
        }
        setNavigationBar()
        //加载微博信息
        MStatusViewModel.sharedInstance.loadStatus { (statuses, error) -> () in
            if error != nil
            {
                MPrint(items: "获取数据失败\(error)")
                return
            }
            MPrint(items: "获取数据成功")
            self.status = statuses
        }
    }
    
    private func setNavigationBar()
    {
        navigationItem.leftBarButtonItem = UIBarButtonItem(image: "navigationbar_pop", target: self, action: "clickLeftBtn")
        navigationItem.rightBarButtonItem = UIBarButtonItem(image: "navigationbar_friendsearch", target: self, action: "clickRightBtn")
        //设置titleView
        let button = MHomeTittleBtn()
        let titleName = MUserAccountViewModel.sharedInstance.userAccount?.screen_name ?? "未知名称"
        button.setTitle(titleName, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "navigationbar_arrow_down"), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(16)
        
        button.addTarget(self, action: "clickTitleView:", forControlEvents: UIControlEvents.TouchUpInside)
        button.sizeToFit()
        
        navigationItem.titleView = button
        
        
    }
    //点击TitleView
    func clickTitleView(button: UIButton){
        button.selected = !button.selected
        UIView.animateWithDuration(0.5) { () -> Void in
            let transfrom = button.selected ? CGAffineTransformMakeRotation(CGFloat(M_PI - 0.001)) : CGAffineTransformIdentity
            
            button.imageView?.transform = transfrom
        }
    }
    
    
    
    //navigation的左右按钮事件
     func clickLeftBtn()
    {
        MPrint(items: "点击了左边")
    }
     func clickRightBtn()
    {
        MPrint(items: "点击了右边")
    }
}
extension MHomeViewController {
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return status?.count ?? 0
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(id) as! M_StatusCell
        let status = self.status![indexPath.row]
        //给cell赋值,去模型
        cell.status = status
        return cell
    }
}
